

cc.Class({
    extends: cc.Component,

    properties: {
        buttonSound: {
            type: cc.AudioClip,
            default: null
        },
        jumpSound: {
            type: cc.AudioClip,
            default: null
        },
        next: {
            type: cc.Node,
            default: null
        },
    },

    ctor() {

    },

    // onLoad () {},

    start () {
        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;
        // cc.director.getCollisionManager().enabledDrawBoundingBox = true;
        // 
        // cc.game.setFrameRate(1)

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(ck.Event.GameStateChanged, this.onGameStateChanged, this)
    },

    onTouchStart() {
        if (!this.data)
            return;

        if (this.data.state == ck.GameState.Finished)
            return

        if (!this.player.canJump()){
            return
        }

        this.player.startJump()
    },

    startLevel(level) {
        ck.log('startLevel', level)

        this.mapNode = this.node.getChildByName('New Node').getChildByName('mapNode')

        this.clearLevel()

        this.data = this.node.addComponent('GameSceneData')
        this.data.init(level)

        this.map = null

        this.next.active = false

        // 加载地图
        var resname = `map${level}`
        cc.loader.loadRes(resname, function (err, res) {
            if (err) {
                ck.log(err)
                cc.director.loadScene("StartScene")
                return
            }

            this.map = cc.instantiate(res)
            this.mapNode.addChild(this.map)

            this.onLoadMapFinished()

        }.bind(this));
    },

    clearLevel(){
        this.data = null
        this.mapNode.stopAllActions()
        this.mapNode.removeAllChildren()
        this.mapNode.position = new cc.Vec3(0,0,0)
        this.border = null
        this.coins = null
        this.player = null
    },

    onLoadMapFinished(){
        ck.log(`加載第${this.data.level}關完成`)

        this.border = this.map.getChildByName('border').getComponent('Border')
        this.coins = this.map.getChildByName('coins')
        this.player = this.map.getChildByName('player').getComponent('Player')

        var rect = new cc.Rect(0, 0,this.border.width(), this.border.height())
        this.followTag = this.mapNode.runAction( cc.follow(this.player.node, rect) )

        this.data.setState(ck.GameState.Gaming)
    },

    onGameStateChanged(){
        if (this.data.state === ck.GameState.Finished) {
            this.onGameFinished()
            this.showEndDialog()
        }
        else if(this.data.state === ck.GameState.Dead) {
            ck.delayCall(this.node, 2, this.onPlayerDead.bind(this))
        }
    },

    onGameFinished() {
        if (this.data.isWin) {
            if (this.data.level>=ck.userData.canPlayLevel) {
                ck.userData.setCanPlayLevel(this.data.level+1)
            }
        }
    },

    showEndDialog(){
        this.next.active = true
    },

    onPlayerDead(){
        this.startLevel(this.data.level)
    },

    onNextLevel() {
        if (this.data.level === ck.userData.maxLevel) {
            cc.director.loadScene("StartScene")
        }
        else{
            this.startLevel(this.data.level+1)
        }
    },

    checkWin() {
        if (this.data.state === ck.GameState.Gaming) {
            if (this.coins.childrenCount === 0) {
                this.data.setState(ck.GameState.FindDoor)
            }
        }
    },

    onClickBack(){
        cc.audioEngine.play(this.buttonSound)

        cc.director.loadScene("StartScene");
    },

    onDirectPass() {
        this.data.setWin(true)
        this.data.setState(ck.GameState.Finished)
    },

    onClickNext() {
        cc.audioEngine.play(this.buttonSound)
        this.onNextLevel()
    },

});
